package com.css.fxfzcpxx.modules.courseManage.controller;

import com.css.fxfzcpxx.base.log.Log;
import com.css.fxfzcpxx.base.response.RestResponse;
import com.css.fxfzcpxx.constants.YNEnum;
import com.css.fxfzcpxx.modules.courseManage.param.CourseManageServiceParam;
import com.css.fxfzcpxx.modules.courseManage.param.CourseManageParam;
import com.css.fxfzcpxx.modules.courseManage.service.CourseManageService;
import com.css.fxfzcpxx.modules.courseManage.vo.CourseManageServiceParamVO;
import com.css.fxfzcpxx.modules.courseManage.vo.GetHistoryTestVO;
import com.css.fxfzcpxx.modules.courseManage.vo.HistoryTestVO;
import com.css.fxfzcpxx.modules.courseManage.vo.TypeManageVO;
import com.css.fxfzcpxx.modules.questionData.entity.UserTestHistory;
import com.css.fxfzcpxx.modules.questionData.vo.FractionalCurveListVO;
import com.css.fxfzcpxx.modules.questionData.vo.FractionalCurveVO;
import com.css.fxfzcpxx.modules.questionData.vo.UserTestHistoryVO;
import com.css.fxfzcpxx.modules.websocket.WebSocketManager;
import com.css.fxfzcpxx.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/03/08
 * @Description: 在线课堂管理Controller
 */
@Api(tags ="在线课堂管理")
@Slf4j
@RestController
@RequestMapping("/courseManage")
public class CourseManageController {
    @Resource
    private CourseManageService courseManageService;

    private final WebSocketManager webSocketManager;

    @Autowired
    public CourseManageController(WebSocketManager webSocketManager) {
        this.webSocketManager = webSocketManager;
    }

    /**
     * 分页列表
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, CourseManageParam param) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = courseManageService.getPage(curPage, pageSize,param);
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 新增单条数据
     * @param param 参数
     * @return
     */
    @PostMapping("/save")
    @Operation(summary = "新增单条数据")
    public RestResponse save(@RequestBody CourseManageServiceParam param) {
        try {
            courseManageService.save(param);
            return RestResponse.succeed("新增成功");
        } catch (Exception e) {
            log.error("新增失败!", e);
            return RestResponse.fail("新增失败!");
        }
    }
    /**
     * 发布
     */
    @PostMapping("/release/{id}")
    @Operation(summary = "发布")
    public RestResponse release(@PathVariable("id") List<String> Id) {
        try {
            courseManageService.updateReleaseStatusById(Id, YNEnum.N.toString());
            webSocketManager.broadcastMessage("在线课堂发布啦","courseManage");
            return RestResponse.succeed("发布成功");
        } catch (Exception e) {
            log.error("发布失败!", e);
            return RestResponse.fail("发布失败!");
        }
    }

    /**
     * 撤回
     */
    @PostMapping("/withdraw/{id}")
    @Operation(summary = "撤回")
    public RestResponse withdraw(@PathVariable("id") List<String> Id) {
        try {
            courseManageService.updateReleaseStatusById(Id,YNEnum.Y.toString());
            webSocketManager.broadcastMessage("在线课堂撤回啦","courseManage");
            return RestResponse.succeed("撤回成功");
        } catch (Exception e) {
            log.error("撤回失败!", e);
            return RestResponse.fail("撤回失败!");
        }
    }

    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/delete/{ids}")
    public RestResponse delete(@PathVariable("ids") String ids){
        RestResponse response = null;
        try{
            courseManageService.delete(ids);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 目录下载
     * @param ids
     */
    @Operation(summary = "目录下载")
    @PostMapping("/exportData/{ids}")
    public void exportData(HttpServletResponse response, @PathVariable("ids") String ids) {
        courseManageService.exportData(response, ids);
    }

    /**
     * 修改单条数据
     * @param param 参数
     * @return
     */
    @PostMapping("/updateById")
    @Operation(summary = "修改单条数据")
    public RestResponse updateById(@RequestBody CourseManageServiceParam param) {
        try {
            courseManageService.updateById(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    /**
     * 根据id查询
     * @param id 主键id
     * @return
     */
    @GetMapping("/findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            CourseManageServiceParamVO data = courseManageService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }
    /**
     * 获取题库type列表与该类型对应题目总数
     * @return
     */
    @GetMapping("/getTypeNum")
    @Operation(summary = "获取题库type列表与该类型对应题目总数")
    public RestResponse getTypeNum(String typeName) {
        try {
            List<TypeManageVO> data = courseManageService.getTypeNum(typeName);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 根据试卷id查看历史试题
     * @param id 试卷id
     * @return
     */
    @GetMapping("/getHistoryTest")
    @Operation(summary = "根据试卷id查看历史试题")
    public RestResponse getHistoryTest(String id) {
        try {
            GetHistoryTestVO data = courseManageService.getHistoryTest(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }
    /**
     * 获取线上分页列表
     * @return
     */
    @GetMapping("/getOnlinePage")
    @Operation(summary = "获取线上分页列表")
    public RestResponse getOnlinePage(HttpServletRequest request) {
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = courseManageService.getOnlinePage(curPage, pageSize);
            return RestResponse.succeed(page);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 根据课堂id查询试历史卷列表
     * @param courseId 课堂id
     * @return
     */
    @GetMapping("/getTestListByCourseId")
    @Operation(summary = "根据课堂id查询试历史卷列表")
    public RestResponse getTestListByCourseId(String id) {
        try {
            List<UserTestHistory> data = courseManageService.getTestListByCourseId(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 查询排行榜
     * @return
     */
    @GetMapping("/getRanking")
    @Operation(summary = "查询排行榜")
    public RestResponse getRanking() {
        try {
            List<UserTestHistoryVO> data = courseManageService.getRanking();
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 查询分数曲线
     * @return
     */
    @GetMapping("/getFractionalCurve")
    @Operation(summary = "查询分数曲线")
    public RestResponse getFractionalCurve() {
        try {
            FractionalCurveListVO data = courseManageService.getFractionalCurve();
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }
}
